package io.renren.modules.moyuData.service.impl;

import io.renren.modules.moyuData.entity.*;
import javafx.scene.chart.Chart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.moyuData.dao.KmgDataDao;
import io.renren.modules.moyuData.service.KmgDataService;


@Service("kmgDataService")
public class KmgDataServiceImpl extends ServiceImpl<KmgDataDao, KmgDataEntity> implements KmgDataService {

    @Autowired
    KmgDataDao kmgDataDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<KmgDataEntity> page = this.page(
                new Query<KmgDataEntity>().getPage(params),
                new QueryWrapper<KmgDataEntity>()
        );

        return new PageUtils(page);
    }

//    @Override
//    public KgmData getTest(){
//        KgmData kgmData = new KgmData();
//        kgmData.setId("123");
//        kgmData.setExperimentCondition("无敌");
//        ChartData chartData = new ChartData();
//        List<SubKgmData> subKgmData = new ArrayList<SubKgmData>();
//        SubKgmData subKgmData1 = new SubKgmData();
//        subKgmData1.setConditionName("one");
//        subKgmData1.setNumber(50.0);
//        List<Double[]> data = new ArrayList<Double[]>();
//        data.add(new Double[]{1.0,2.0});
//        data.add(new Double[]{1.0,2.0});
//        subKgmData1.setData(data);
//        subKgmData.add(subKgmData1);
//        subKgmData.add(subKgmData1);
//        chartData.setSeries(subKgmData);
//        kgmData.setChartData(chartData);
//        return kgmData;
//
//    }
    @Override
    public List<KgmData> getPreparationMethod(Map<String,Object> params){
        List<KgmData>list=new ArrayList<KgmData>();
        String kgmState=(String) params.get("kgmState");
        String kgmBlendingEnvironment=(String) params.get("kgmBlendingEnvironment");
        String dataType=(String) params.get("dataType");
        Double temperature=null;
        Double ph=null;
        Double concentration=null;
        if (params.get("temperature")!=null){
            temperature=Double.parseDouble((String) params.get("temperature"));
        }
        if(params.get("ph")!=null){
            ph=Double.parseDouble((String) params.get("ph"));
        }
        if(params.get("concentration")!=null){
            concentration=Double.parseDouble((String) params.get("concentration"));
        }
        //从数据库查出所有符合条件的kmgDataEntity
        List<KmgDataEntity>kmgDataEntityList=kmgDataDao.getPreparationMethod(kgmState,kgmBlendingEnvironment,dataType,temperature,ph,concentration);
        if (!kmgDataEntityList.isEmpty()){
            //根据id 对所有数据进行分组
            Map<String, List<KmgDataEntity>> map=kmgDataEntityList.stream().collect(Collectors.groupingBy(KmgDataEntity::getId));
            //遍历map 每组转成一个KgmData
            map.forEach((str,sublist)->{
                //一个id对应一个KgmData
                KgmData kgmData=new KgmData();
                //先把kgmData的id ex 赋值
                kgmData.setId(str);
                kgmData.setExperimentCondition(sublist.get(0).getExperimentCondition());
                //通过sublist 构造 chartData
                ChartData chartData=new ChartData();
                List<SubKgmData> series=new ArrayList<SubKgmData>();
                //一个分组list里面有多组subDATA,构造每个subdata
                kmgDataEntityList.forEach(item->{
                    SubKgmData subKgmData=new SubKgmData();
                    List<Double[]> data=new ArrayList<Double[]>();
                    //查询坐标
                    List<Coordinate>coordinates=kmgDataDao.getCoordinate(item.getKgmId());
                    //遍历坐标链表，并赋值给data
                    coordinates.forEach(it->{
                        data.add(new Double[]{it.getX(),it.getY()});
                    });
                    subKgmData.setData(data);
                    subKgmData.setNumber(item.getNumber());
                    subKgmData.setConditionName(item.getConditionName());
                    series.add(subKgmData);
                });
                chartData.setSeries(series);
                //完全体KgmData
                kgmData.setChartData(chartData);
                list.add(kgmData);
            });
        }


//        List<KgmData>kgmDataPOList=kmgDataEntityList.stream().map(item->{
//            KgmData kgmData=new KgmData();
//            ChartData chartData=new ChartData();
//            SubKgmData subKgmData=new SubKgmData();
//            List<Double[]> data=new ArrayList<Double[]>();
//            //查询坐标
//            List<Coordinate>coordinates=kmgDataDao.getCoordinate(item.getKgmId());
//            //遍历坐标链表，并赋值给data
//            coordinates.forEach(it->{
//                data.add(new Double[]{it.getX(),it.getY()});
//            });
//            //大功告成，从小到大给对象赋值
//            //SubKgmData
//            subKgmData.setData(data);
//            subKgmData.setNumber(item.getNumber());
//            subKgmData.setConditionName(item.getConditionName());
//            //chartData
//
//            return kgmData;
//        }).collect(Collectors.toList());
        return list;
    }



}